﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace LibSpeedShot
{
	public partial class CaptureForm : Form
	{
		private Rectangle _selRegion = new Rectangle(0, 0, 0, 0);
		private bool capturing = false;
		private Point startPoint;
		private Point prevEndPoint;
		private Point endPoint;
		private Bitmap memImage = null;
		private Graphics imageDC = null;

		private Pen p = null;
		private Brush b = null;

		public CaptureForm()
		{
			InitializeComponent();
		}

		private void CaptureForm_Load(object sender, EventArgs e)
		{
			p = new Pen(Color.Blue, 5);
			b = new SolidBrush(Color.White);	//transparent
			memImage = new Bitmap(this.Width, this.Height);
			imageDC = Graphics.FromImage(memImage);
			FillImage();
		}

		public Rectangle SelectedRegion
		{
			get
			{
				return _selRegion;
			}
			set
			{
				_selRegion = value;
			}
		}

		private void CaptureForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == (char)Keys.Escape)
			{
				this.Close();
			}
		}

		private void CaptureForm_MouseDown(object sender, MouseEventArgs e)
		{
			capturing = true;
			startPoint = new Point(e.X, e.Y);

		}

		private void CaptureForm_MouseMove(object sender, MouseEventArgs e)
		{
			if (capturing)
			{
				prevEndPoint.X = endPoint.X;
				prevEndPoint.Y = endPoint.Y;
				endPoint = new Point(e.X, e.Y);
				this.Invalidate(GetRect(startPoint, prevEndPoint));
			}
		}

		private void CaptureForm_MouseUp(object sender, MouseEventArgs e)
		{
			capturing = false;
			_selRegion = GetRect(startPoint, endPoint);
			this.Close();
		}

		private void FillImage()
		{
			imageDC.FillRectangle(b, 0, 0, memImage.Width, memImage.Height);
		}

		private Rectangle GetRect(Point start, Point end)
		{
			Rectangle rect = new Rectangle();
			if(start.X < end.X)
			{
				rect.X = start.X;
				rect.Width = end.X - start.X;
			}
			else 
			{ 
				rect.X = end.X;
				rect.Width = start.X - end.X;
			}

			if (start.Y < end.Y)
			{
				rect.Y = start.Y;
				rect.Height = end.Y - start.Y;
			}
			else
			{
				rect.Y = end.Y;
				rect.Height = start.Y - end.Y;
			}
			
			return rect;
		}

		protected override void OnPaint(PaintEventArgs e)
		{
            base.OnPaint(e);
            if (capturing)
            {
                Graphics g = e.Graphics;
                Rectangle rect = GetRect(startPoint, endPoint);
                g.CompositingMode = CompositingMode.SourceOver;
                g.CompositingQuality = CompositingQuality.HighSpeed;
                g.DrawImage(memImage, rect, rect, GraphicsUnit.Pixel);
            }
            else { base.OnPaint(e); }
		}

		private void CaptureForm_Resize(object sender, EventArgs e)
		{
			if (memImage != null)	//wait for Form Load to create the image.
			{
				memImage = new Bitmap(memImage, this.Width, this.Height);
			}
		}

		private void CaptureForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			memImage.Dispose();
			imageDC.Dispose();
		}
	}
}
